import 'dart:convert';

import 'package:elephant_fish/TRTCLiveRoomDemo/ui/room/LiveRoomPage.dart';
import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/gift_classify_request.dart';
import 'package:elephant_fish/model/gift_classify_entity.dart';
import 'package:elephant_fish/page/index_hot_page.dart';
import 'package:elephant_fish/page/login_page.dart';
import 'package:elephant_fish/page/one_gift_page.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class BroastGift extends StatefulWidget {
  final int roomId;
  final bool isShield;
  final int userId;
  const BroastGift({Key key, this.roomId, this.isShield, this.userId}) : super(key: key);

  @override
  _BroastGiftState createState() => _BroastGiftState();
}

class _TabData {
  final Widget tab;
  final Widget body;
  _TabData({this.tab, this.body});
}


 List<_TabData> _tabDataList = [];
int sum = 0;
class _BroastGiftState extends State<BroastGift> {
  List<GiftClassifyEntity> list= [];

  @override
  void initState() {
    super.initState();
    getGiftClassify();
  }
  var tabBarList = _tabDataList.map((item) => item.tab).toList();
  var tabBarViewList = _tabDataList.map((item) => item.body).toList();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0x660c0c0c),
      body: DefaultTabController(
        length: tabBarList.length,
        child: Scaffold(
          appBar: AppBar(
            automaticallyImplyLeading: false,
            actions: [
            ],
            backgroundColor: Color(0x660c0c0c),
            title: Row(
              children: [
                Expanded(child: TabBar(
                  // isScrollable: true, //可滚动
                  indicatorColor: Color(0xffff215d), //指示器的颜色
                  labelColor: Color(0xffff215d), //选中文字颜色
                  unselectedLabelColor: Color(0xfff3f3f3), //为选中文字颜色
                  isScrollable: true,
                  unselectedLabelStyle: TextStyle(
                    fontSize: SizeUtil.px(30),
                  ),
                  labelStyle: TextStyle(
                    fontSize: SizeUtil.px(30),
                  ),
                  // indicatorSize: TabBarIndicatorSize.label, //指示器与文字等宽
                  tabs: tabBarList,
                ),

                ),

              ],
            ),
          ),
          body:
          TabBarView(
              children: tabBarViewList
          ),
        ),
      ),
    );
  }

  Future<void> getGiftClassify() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = GiftClassifyRequest();
    request.addHeader('usertoken', appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(arguments: {'loginCode':'-1'},);
        },
      ));
    }else if(result['code'] == 0){

      List<Map<String,dynamic>> listMap = new List<Map<String, dynamic>>.from(result['data']['data']);
      for(int i = 0 ; i < listMap.length ; i ++){
        list.add(GiftClassifyEntity.fromJson(listMap[i]));
      }
      sum ++ ;
      for(int i = 0; i < list.length;i ++){
        if(sum == 1){
          _tabDataList.add( _TabData(tab: Text(list[i].name), body: OneGiftPage(categoryName: list[i].id,roomId: widget.roomId,isShield: widget.isShield,userId: widget.userId,)));
        }
      }
      setState(() {
        tabBarList = _tabDataList.map((item) => item.tab).toList();
        tabBarViewList = _tabDataList.map((item) => item.body).toList();
      });
    }
  }
}
